<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务令" prop="po">
        <el-input v-model="queryParams.po" placeholder="请输入任务令" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品编码" prop="itemCode" label-width="88px">
        <el-input v-model="queryParams.itemCode" placeholder="请输入产品编码" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品条码" prop="barCode" label-width="88px">
        <el-input v-model="queryParams.barCode" placeholder="请输入产品条码" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="物料编码" prop="sonItemCode">
        <el-input v-model="queryParams.sonItemCode" placeholder="请输入物料编码" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="上传标识" prop="uploadFlag">
        <el-input v-model="queryParams.uploadFlag" placeholder="请输入上传标识" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="华为任务令" prop="segment3" label-width="88px">
        <el-input v-model="queryParams.segment3" placeholder="请输入华为任务令" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="物料条码SN" prop="sonBarCode" label-width="88px">
        <el-input v-model="queryParams.sonBarCode" placeholder="请输入物料条码SN" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="采集时间" prop="createdDate" label-width="88px">
        <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期"
                        end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss"
                        :default-time="['00:00:00', '23:59:59']">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="segment5" prop="segment5" label-width="88px">
        <el-input v-model="queryParams.segment5" placeholder="请输入segment5" clearable size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="checkedList.length==0"
                   @click="handleUntie">解绑
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="barcodeRelationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" :selectable='checkbox'/>
      <el-table-column label="任务令" align="center" prop="po"/>
      <el-table-column label="供应商编码" align="center" prop="vendorId" width="100"/>
      <el-table-column label="orgId" align="center" prop="orgId"/>
      <el-table-column label="产品编码" align="center" prop="itemCode" width="100"/>
      <el-table-column label="产品条码" align="center" prop="barCode"/>
      <el-table-column label="物料条码SN" align="center" prop="sonBarCode" width="100"/>
      <el-table-column label="物料编码" align="center" prop="sonItemCode"/>
      <el-table-column label="实际物料数量" align="center" prop="quantity" width="100"/>
      <el-table-column label="供应商名称" align="center" prop="supplier" width="100"/>
      <el-table-column label="制造商名称" align="center" prop="manufactory" width="100"/>
      <el-table-column label="上传标识" align="center" prop="uploadFlag">
        <template slot-scope="scope">
                    <span>{{
                        scope.row.uploadFlag === 0 ? '待上载' : scope.row.uploadFlag === 1 ? '正在上载' : scope.row.uploadFlag === 2 ? '上载成功' : scope.row.uploadFlag === 9 ? '上载失败' : ''
                      }}</span>
        </template>
      </el-table-column>
      <el-table-column label="华为接口表返回校验信息" align="center" prop="checkMessage" width="180"/>
      <el-table-column label="对应关系记录创建者" align="center" prop="createdBy" width="180"/>
      <el-table-column label="采集时间" align="center" prop="createdDate"/>
      <el-table-column label="segment1" align="center" prop="segment1" width="100"/>
      <el-table-column label="segment2" align="center" prop="segment2" width="100"/>
      <el-table-column label="华为任务令" align="center" prop="segment3" width="100"/>
      <el-table-column label="segment4" align="center" prop="segment4" width="100"/>
      <el-table-column label="segment5" align="center" prop="segment5" width="100">
        <template slot-scope="scope">
                    <span>{{
                        scope.row.segment5 === 1 ? '已解绑' : '已回传'
                      }}</span>
        </template>
      </el-table-column>
      <el-table-column label="emsTransId" align="center" prop="emsTransId" width="100"/>
      <el-table-column label="物料编码" align="center" prop="segment10" width="100"/>
      <el-table-column label="产品编码" align="center" prop="segment11"/>
      <el-table-column label="segment12" align="center" prop="segment12" width="100"/>
      <el-table-column label="segment13" align="center" prop="segment13" width="100"/>
      <el-table-column label="segment14" align="center" prop="segment14" width="100"/>
      <el-table-column label="segment15" align="center" prop="segment15" width="100"/>
      <!-- <el-table-column label="华为回传结果" align="center" prop="res" width="100" /> -->
      <el-table-column label="华为回传结果" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDetails(scope.row)"> 查看</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetails(scope.row)"
                 >华为回传详情</el-button>

          </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <el-dialog
      title="华为回传结果"
      :visible.sync="dialogVisible"
      width="30%"
    >

      {{ details.res ? JSON.parse(details.res) : '暂无' }}

    </el-dialog>
  </div>
</template>

<script>
import {
  unbindingRelation,
  listbarcodeRelation,
  getbarcodeRelation,
  delbarcodeRelation,
  addbarcodeRelation,
  updatebarcodeRelation,
  exportbarcodeRelation
} from "@/api/ems/barcodeRelation";

export default {
  name: "barcodeRelation",
  data() {
    return {
      checkedList: [],
      // 遮罩层
      loading: true, dateRange: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品关系追溯表格数据
      barcodeRelationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        po: undefined,
        itemCode: undefined,
        barCode: undefined,
        sonItemCode: undefined,
        uploadFlag: undefined,
        segment3: undefined,
        segment5: undefined,
        sonBarCode: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dialogVisible: false,
      details: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkbox(row, rowIndex) {
      if (row.segment5 == 1) {
        return false;//禁用
      } else {
        return true;//不禁用
      }
    },
    handleDetails(val) {
      this.dialogVisible = true
      this.details = val

    },
    // 解绑
    handleUntie() {
      console.log(this.checkedList);
      unbindingRelation(this.checkedList).then(() => {
        this.msgSuccess("解绑成功");
      }).catch(() => {
        this.msgError("解绑失败");
      })
    },
    /** 查询物料产品关系追溯列表 */
    getList() {
      this.loading = true;
      listbarcodeRelation(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.barcodeRelationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.checkedList = selection
      this.ids = selection.map(item => item.po)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料产品关系追溯";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const po = row.po || this.ids
      getbarcodeRelation(po).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料产品关系追溯";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.po != null) {
            updatebarcodeRelation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addbarcodeRelation(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pos = row.po || this.ids;
      this.$confirm('是否确认删除物料产品关系追溯编号为"' + pos + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delbarcodeRelation(pos);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有物料产品关系追溯数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportbarcodeRelation(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () {
      });
    }
  }
};
</script>
